#include "find.h"
#define COUNT 107
int* arr = new int[COUNT];
//记录成绩个数
int size = 0;
wanghao::Find f;
//全局函数指针数组
int (wanghao::Find::*ptr[])(int*,int,int) = {nullptr,
                    &wanghao::Find::SequentialSearch,
                    &wanghao::Find::BinarySearch, 
                    &wanghao::Find::BlockSearch,
                    &wanghao::Find::InterpolationSearch,
                    &wanghao::Find::FibonacciSearch};
//学生成绩管理系统枚举
enum Display
{
    EXIT,
    ADD,
    DEL,
    MODIFY,
    FIND,
    SORT,
    SHOW,
    SCORE
};
//学生成绩查询系统枚举
enum Show
{
    QUIT,
    SEQUENTIALSEARCH,
    BIMARYSEARCH,
    BLOCKSEARCH,
    INTERPOLATIONSERCH,
    FIBONACCISEARCH,
    DISPLAYMENU,
};
//查找算法枚举
enum
{
    SEQUENTIALALGORITHM = 1,
    BINARYALGORITHM,
    BLOCKALGORITHM,
    INTERPOLATIONALGORITHM,
    FIBONACCIALGORITHM
};
//学生成绩查询系统显示界面
void ShowMenu()
{
    cout << "-------------------------------------" << endl;
    cout << "           学生成绩查询系统            " << endl;
    cout << "-------------------------------------" << endl;
    cout << "             0.退出                  " << endl;
    cout << "             1.顺序查找               " << endl;
    cout << "             2.折半查找               " << endl;
    cout << "             3.分块查找               " << endl;
    cout << "             4.插值查找               " << endl;
    cout << "             5.斐波那契               " << endl;
    cout << "             6.菜单显示               " << endl;
    cout << "-------------------------------------" << endl;
}
//学生成绩管理系统显示界面
void show()
{
    cout << "-------------------------------------" << endl;
    cout << "           学生成绩管理系统            " << endl;
    cout << "-------------------------------------" << endl;
    cout << "             0.退出                  " << endl;
    cout << "             1.添加成绩               " << endl;
    cout << "             2.删除成绩               " << endl;
    cout << "             3.修改成绩               " << endl;
    cout << "             4.查询成绩               " << endl;
    cout << "             5.成绩排序               " << endl;
    cout << "             6.菜单显示               " << endl;
    cout << "             7.显示成绩               " << endl;
    cout << "-------------------------------------" << endl;
}
//学生成绩界面显示
void showScore()
{
    cout << "|---------------------------------------|" << endl;
    cout << "                 学生成绩                " << endl;
    cout << "|---------------------------------------|" << endl;
    for(int i = 0;i < size;i++)
    {
        if(i % 10 == 0 && i != 0)
        {
            cout << endl;
            cout << "-----------------------------------------" << endl;
            //cout << endl;
        } 
        printf("|%3d",*(arr + i));
    }
    printf("\n");
}
//暴力查找
void Sequential()
{

    showScore();
    int score = 0;
    cout << "请输入要查找的成绩:>";
    cin >> score;
    //调用函数指针数组
    int find = (f.*ptr[SEQUENTIALALGORITHM])(arr,size,score);
    if(find != -1)
    {
        cout << "该成绩找到了,在第" << find + 1 << "个位置" << endl;
    }
    else cout << "该成绩没有找到" << endl;
}
//折半查找
void Binary()
{
    int* scoreArr = new int[size];
    for(int i = 0;i < size;i++)
    {
        *(scoreArr + i) = *(arr + i);
    }
    f.QuickSort(scoreArr,size);
    int score = 0;
    cout << "请输入要查询的成绩:>";
    cin >> score;
    int find = (f.*ptr[BINARYALGORITHM])(scoreArr,size,score);
    if(find != -1)
    {
        cout << "该成绩存在" << endl;
    }
    else cout << "该成绩没有找到" << endl;
    delete[] scoreArr;
}
//分块查找
void Block()
{
    int* scoreArr = new int[size];
    for(int i = 0;i < size;i++)
    {
        *(scoreArr + i) = *(arr + i);
    }
    f.QuickSort(scoreArr,size);
    int score = 0;
    cout << "请输入要查询的成绩:>";
    cin >> score;
    int find = (f.*ptr[BLOCKALGORITHM])(scoreArr,size,score);
    if(find != -1)
    {
        cout << "该成绩存在" << endl;
    }
    else cout << "该成绩没有找到" << endl;
    delete[] scoreArr;
}
//插值查找
void Interpolation()
{
    int* scoreArr = new int[size];
    for(int i = 0;i < size;i++)
    {
        *(scoreArr + i) = *(arr + i);
    }
    f.QuickSort(scoreArr,size);
    int score = 0;
    cout << "请输入要查询的成绩:>";
    cin >> score;
    int find = (f.*ptr[INTERPOLATIONALGORITHM])(scoreArr,size,score);
    if(find != -1)
    {
        cout << "该成绩存在" << endl;
    }
    else cout << "该成绩没有找到" << endl;
    delete[] scoreArr;
}
//斐波那契查找
void Fibonacci()
{
    int* scoreArr = new int[size];
    for(int i = 0;i < size;i++)
    {
        *(scoreArr + i) = *(arr + i);
    }
    f.QuickSort(scoreArr,size);
    int score = 0;
    cout << "请输入要查询的成绩:>";
    cin >> score;
    int find = (f.*ptr[FIBONACCIALGORITHM])(scoreArr,size,score);
    if(find != -1)
    {
        cout << "该成绩存在" << endl;
    }
    else cout << "该成绩没有找到" << endl;
    delete[] scoreArr;  
}
//学生成绩查询系统逻辑代码
void ScoreFind()
{
    ShowMenu();//显示界面
    int input = 0;
    do
    {
        cout << "请输入选项:>";
        cin >> input;
        switch(input)
        {
            case QUIT:
                cout << "已经退出学生成绩查询系统,进入学生成绩管理系统!!!" << endl;
                break;
            case SEQUENTIALSEARCH:
                //顺序查找
                Sequential();
                break;
            case BIMARYSEARCH:
                //折半查找
                Binary();
                break;
            case BLOCKSEARCH:
                //分块查找
                Block();
                break;
            case INTERPOLATIONSERCH:
                //插值查找
                Interpolation();
                break;
            case FIBONACCISEARCH:
                //斐波那契查找
                Fibonacci();
                break;
            case DISPLAYMENU:
                ShowMenu();
                break;
            default:
                cout << "输入错误!!!" << endl;
                break;
        }
    }while(input);
}
//添加成绩逻辑代码
void AddScore()
{
    int count = 0;
    int score = 0;
    char c = '\0';
    cout << "请输入要插入的成绩的个数:>";
    cin >> count;
    cout << "请输入" << count << "个成绩:>";
    int index = 0;
    //while((c = getchar()) != '\n' && c != EOF);
    for(int i = count;i > 0;i--)
    {
        scanf("%d",&score);
        *(arr + index++) = score;
    }
    size += count;
    cout << "成绩添加完成!!!" << endl;
}
//删除成绩逻辑代码
void DelScore()
{
begin:
    cout << "删除成绩的范围只能是1~" << size <<",否则会越界" << endl;
    showScore();
    int del = 0;
    cout << "请输入要删除第几个成绩:>";
    cin >> del;
    if(del >= 1 && del <= size)
    {
        for(int i = del - 1;i < size;i++)
        {
            *(arr + i) = *(arr + i + 1);
        }
        size -= 1;
    }
    else
    {
        cout << "越界了哦,请重新输入" << endl;
        goto begin;
    } 
    cout << "成绩删除完成!!!" << endl;
}
//修改成绩逻辑代码
void ModifyScore()
{
    begin:
    cout << "修改成绩的范围只能是1~" << size <<",否则会越界" << endl;
    showScore();
    int modify = 0;
    cout << "请输入要修改第几个成绩:>";
    cin >> modify;
    if(modify >= 1 && modify <= size)
    {
        cout << "请输入第" << modify << "个成绩修改之后的成绩:>";
        int value = 0;
        cin >> value;
        *(arr + modify - 1) = value;
    }
    else
    {
        cout << "越界了哦,请重新输入" << endl;
        goto begin;
    }
    cout << "成绩修改完成!!!" << endl;
}
//学生成绩管理系统逻辑代码
void FindAlgorithm()
{
    show();
    int input = 0;
    do
    {
        cout << "请输入选项:>";
        cin >> input;
        switch(input)
        {
            case EXIT:
                cout << "已经退出学生成绩管理系统!!!" << endl;
                break;
            case ADD:
                //添加成绩
                AddScore();
                break;
            case DEL:
                //删除成绩
                DelScore();
                break;
            case MODIFY:
                //修改成绩
                ModifyScore();
                break;
            case FIND:
                //查找成绩
                cout << "进入学生成绩查询系统!!!" << endl;
                ScoreFind();
                break;
            case SORT:
                //对成绩排序
                f.QuickSort(arr,size);
                cout << "成绩排序完成" << endl;
                break;
            case SHOW:
                show();
                break;
            case SCORE:
                //显示成绩
                showScore();
                break;
            default:
                cout << "输入错误!!!" << endl;
                break;
        }
    }while(input);
    delete[] arr;
}
int main()
{
    //调用学生成绩管理系统
    FindAlgorithm();
    return 0;
}
